﻿// ———————————————————————–
// <copyright file="RootNodeNameMessageFilter.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// RootNodeNameMessageFilter.cs - "An Interface For Filtering Messages"
// Project: "IC.NETSOA"- "ICNETCoreRouter"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
                
using System;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Xml.Linq;

namespace ICNETCoreRouter
{
  /// <summary>
  /// Contains the methods that need to be overriden to filter Messages and MessageBuffers. 
  /// </summary>
  public class RootNodeNameMessageFilter : MessageFilter
  {
    /// <summary>
    /// The namespace used.
    /// </summary>
    private XNamespace xs;

    /// <summary>
    /// The filter against which messages will be matched.
    /// </summary>
    private string filterData;

    /// <summary>
    /// Initializes a new instance of the RootNodeNameMessageFilter class. 
    /// </summary>
    /// <param name="filterData">The data that is used to construct the message filter.</param>
    public RootNodeNameMessageFilter(string filterData)
    {
      int index;
      if (string.IsNullOrEmpty(filterData))
      {
        throw new ArgumentException("filterData");
      }

      if (filterData.Contains(";"))
      {
        index = filterData.IndexOf(";");
        this.xs = filterData.Substring(0, index);
        this.filterData = filterData.Substring((index + 1));
      }
      else
      {
        this.filterData = filterData;
      }
    }

    /// <summary>
    /// Attempts to match a message using the initially given filter criteria. 
    /// </summary>
    /// <remarks>Must be overriden. </remarks>
    /// <param name="message">The message to be matched.</param>
    /// <returns>false if the message did not match up against the filter data, true if it did.</returns>
    public override bool Match(Message message)
    {
      return false;
    }

    /// <summary>
    /// Matches a MessageBuffer against the intitially given filter criteria. 
    /// </summary>
    /// <remarks>Must be overriden.</remarks>
    /// <param name="buffer">The MessageBuffer to be matched.</param>
    /// <returns>True if the MessageBuffer was matched, false if it was not.</returns>
    public override bool Match(MessageBuffer buffer)
    {
      XElement xe;
      xe = buffer.CreateMessage().GetBody<XElement>();
      var gobble =
          from el in xe.AncestorsAndSelf(this.xs + this.filterData)
          select el;
      int i = gobble.Count();

      if (i > 0)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
  }
}
